package com.zhilun;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.*;

@Slf4j
public class SensorBind {
    public static final String key ="ade0ccb442c945fb939a2f82193f6cdf";
    public static final String secret ="MDEwMzE1MjY2MDVjMWUxOTAxNWM2N2I2OGZiYjJiYzc=";

    // 构造URL
    public static final String url = "https://open-p.zhilunkeji.com";
    public static final String api ="/open/api/wheels/opt/installSensorNo";


    /**
     * key:ade0ccb442c945fb939a2f82193f6cdf
     * secret:MDEwMzE1MjY2MDVjMWUxOTAxNWM2N2I2OGZiYjJiYzc=
     * @param args
     */
    public static void main(String[] args) {


        // 请求参数
//      String carrierId = "33968"; //皖MT530挂
//        String carrierId = "90185"; //浙AP160挂
        String carrierId = "89442"; //皖CEE58挂


        String sensorNo = "22149932"; //皖MT530挂
//        String sensorNo = "0123458D"; //皖MT530挂
        String tyrePosition = "L0101";//轮位
        String operator = "黄超凡";




        // 构造签名
        Map<String, String[]> paramMap = new HashMap<>();
        paramMap.put("accessKey", new String[] {key});

//        paramMap.put("pageSize", new String[] {pageSize});
       // paramMap.put("alarm", alarm);
   //     paramMap.put("timestamp", new String[] {System.currentTimeMillis() +""});
        String sign = signParameterMap(paramMap, secret);
        System.out.println("sign: " +sign);

        paramMap.put("sign", new String[]{sign});

        // 构造参数Map
        Map<String, Object> param = new HashMap<>();
        param.put("carrierId", carrierId);
        param.put("sensorNo", sensorNo);
        param.put("tyrePosition", tyrePosition);
        param.put("operator", operator);



        // 手动拼接URL和参数
        String fullUrl = url +api + "?" +sign;

        // 打印拼接后的URL
        System.out.println("Full URL: " + fullUrl);

        // 使用Hutool发送GET请求
        HttpRequest request = HttpUtil.createPost(fullUrl).body(JSON.toJSONString(param));

        System.out.println("body: " );
        System.out.println(JSON.toJSONString(param));
        // 执行请求并获取响应
        HttpResponse response = request.execute();

        // 获取响应结果
        String result = response.body();
        System.out.println("Response: " );
        System.out.println(result);
    }



    public static String signParameterMap(Map<String, String[]> parametersMap, String secret) {
        //添加时间戳，排序并删除空值
        Map<String, String[]> unlock = new HashMap<>(parametersMap);
        unlock.put("timestamp", new String[]{String.valueOf(System.currentTimeMillis() / 1000)});
        List<String> keys = new ArrayList<>(unlock.keySet());
        Collections.sort(keys);
        StringBuilder signStr = new StringBuilder();
        for (String key : keys) {
            String[] values = unlock.get(key);
            if (values.length < 1) {
                continue;
            }
            for (String v : values) {
                signStr.append(key).append("=").append(v).append("&");
            }
        }
        signStr.deleteCharAt(signStr.lastIndexOf("&"));
        String sign = DigestUtils.md5Hex(signStr.toString() + secret);
        signStr.append("&sign=").append(sign);
        return signStr.toString();
    }





}
